//
//  FTTabBar.swift
//  Weibo
//
//  Created by 方焘 on 16/6/11.
//  Copyright © 2016年 taofang. All rights reserved.
//

import UIKit

protocol FTTabBarDelegate:NSObjectProtocol {
   func didClickCompseButton()
}

class FTTabBar: UITabBar {
    //MARK: 外部访问属性
   weak var FTDelegate: FTTabBarDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        self.addSubview(composeButton)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let width = UIScreen.mainScreen().bounds.size.width / 5
        let height = self.frame.size.height
        var index: CGFloat = 0
        
        for view in subviews {
            if view.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                view.frame = CGRect(x: width * index, y: 0, width: width, height: height)
                index++
                if index == 2 {
                    index++
                }
            }
            
        }
        composeButton.center.x = self.frame.size.width / 2
        composeButton.center.y = self.frame.size.height / 2
        composeButton.bounds.size.height = height
        composeButton.bounds.size.width = width
    }
    
    private lazy var composeButton:UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
        button.addTarget(self, action:#selector(FTTabBar.didClickCompseButton), forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()
        return button
    }()
    
   @objc private func didClickCompseButton() {
        FTDelegate?.didClickCompseButton()
    }

}
